package orm;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {

    static Properties pros=null;//可以帮助读取和处理资源文件中的信息
    //静态代码块,只会在类被加载的时候加载一次
    static {
        pros=new Properties();
        try {
            //读bin目录下面的资源文件
            pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db2.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getMysqlConn(){
        Connection conn=null;
        try {
            Class.forName(pros.getProperty("mysqlDriver"));
            conn= DriverManager.getConnection(pros.getProperty("mysqlUrl"),pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd"));
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void close(ResultSet rs, Statement ps, Connection conn){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement ps, Connection conn){
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Connection conn){
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
